package com.bawei.module_my.reg;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bawei.library_base.Api;
import com.bawei.library_base.RouterPath;
import com.bawei.library_base.entity.RegLogUser;
import com.bawei.library_base.entity.User;
import com.bawei.library_base.manager.MyManager;
import com.bawei.library_base.mvp.v.BaseActivity;
import com.bawei.module_my.R;
import com.bawei.module_my.log.LogActivity;
import com.blankj.utilcode.util.ToastUtils;

import org.jetbrains.annotations.NotNull;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

@Route(path = RouterPath.Reg.REG)
public class RegActivity extends BaseActivity {


    private TextView outReg;
    private EditText mMobileEt;
    private EditText mVerifyCodeEt;
    private EditText mPwdEt;
    private EditText mPwdConfirmEt;
    private Button mRegisterBtn;
    private ImageView yzm;

    private String isyzm;

    @Override
    public int beanLayout() {
        return R.layout.activity_reg;
    }

    @Override
    public void initView() {

        outReg = (TextView) findViewById(R.id.out_reg);
        mMobileEt = (EditText) findViewById(R.id.mMobileEt);
        mVerifyCodeEt = (EditText) findViewById(R.id.mVerifyCodeEt);
        mPwdEt = (EditText) findViewById(R.id.mPwdEt);
        mPwdConfirmEt = (EditText) findViewById(R.id.mPwdConfirmEt);
        mRegisterBtn = (Button) findViewById(R.id.mRegisterBtn);
        yzm = (ImageView) findViewById(R.id.yzm);
    }

    @Override
    public void initData() {

        //退出注册
        outreg();
        //注册--获取验证码
        pdyzm();
        //注册
        reg();


    }

    private void reg() {
        mRegisterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyManager.getInstance().getRetrofit("http://82.156.178.182:8082/")
                        .create(Api.class)
                        .reg(new RegLogUser(mPwdEt.getText().toString(),mPwdConfirmEt.getText().toString()))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<User>() {
                            @Override
                            public void onSubscribe(@NotNull Disposable d) {

                            }

                            @Override
                            public void onNext(@NotNull User user) {
                                ToastUtils.showShort("111");
                                    if (user.getCode()==200){
                                            //成功
                                        ToastUtils.showShort("成功");
                                        Intent intent = new Intent(RegActivity.this, LogActivity.class);
                                        startActivity(intent);
                                        finish();
                                    }else if (!mVerifyCodeEt.getText().toString().equals(isyzm)){
                                        //验证码错误
                                        Toast.makeText(RegActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();

                                        ToastUtils.showShort("验证码错误");
                                    }else if (user.getCode()==-1){
                                        //用户存在
                                        Toast.makeText(RegActivity.this, "用户存在", Toast.LENGTH_SHORT).show();
                                        ToastUtils.showShort("用户存在");

                                    }else {
                                        ToastUtils.showShort("失败");
                                    }
                            }

                            @Override
                            public void onError(@NotNull Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        });
    }

    private void pdyzm() {
        yzm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {


                MyManager.getInstance().getRetrofit("http://82.156.178.182:8082/")
                        .create(Api.class)
                        .phone()
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(@NotNull Disposable d) {

                            }

                            @Override
                            public void onNext(@NotNull String s) {
                                isyzm=s;
                                Notification build = new Notification.Builder(RegActivity.this).setSmallIcon(R.drawable.icon_phone)
                                        .setContentTitle("上码")
                                        .setContentText("验证码" + s)
                                        .build();

                                NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                                manager.notify(1,build);

                            }

                            @Override
                            public void onError(@NotNull Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        });
    }

    private void outreg() {
        outReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.finish();
    }
}